// pages/goods_list/index.js
import { request } from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //页面导航
    tabs: [
      {
        id: 0,
        value: "综合",
        isActive: true
      },
      {
        id: 1,
        value: "销量",
        isActive: false
      },
      {
        id: 2,
        value: "价格",
        isActive: false
      }
    ],
    goodsList: []
  },
  //接口需要的参数
  queryParams: {
    query: "",
    cid: "",
    pagenum: 1,
    pagesize: 10
  },
  handleTabsItemChange (e) {
    const { index } = e.detail;
    let { tabs } = this.data;
    tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false)
    this.setData({
      tabs
    })
  },
  ListPage: 1,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryParams.cid = options.cid || ""
    this.queryParams.query = options.query || ""
    this.getGoodsList()

  },
  async getGoodsList () {

    const res = await request({ url: "/goods/search", data: this.queryParams })
    const total = res.total;
    this.ListPage = Math.ceil(total / this.queryParams.pagesize);
    console.log(this.ListPage)
    this.setData({
      goodsList: [...this.data.goodsList, ...res.goods]
    })

    //请求到数据后关闭刷新
    wx.stopPullDownRefresh();

  },
  //页面上划 滚动条触底事件
  onReachBottom () {

    if (this.queryParams.pagenum >= this.ListPage) {
      // console.log("没有下一页了")
      wx.showToast({
        title: '没有下一页数据了',
      })
    } else {
      this.queryParams.pagenum++;
      this.getGoodsList();
    }
  },
  //下拉刷新事件
  onPullDownRefresh () {
    this.setData({
      goodsList: []
    })
    this.getGoodsList();
  }
})